﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using System.IO;
using System.Collections;

namespace FisheryPlatform.Search.Core.Analysis
{
    /// <summary>
    /// 长度优先分词器,用于汉字等域的分词
    /// </summary>
    public class MMAnalyzer : Analyzer
    {
        public FilterFactory FilterFactory
        { get; set; }

        public DictionaryFactory DictionaryFactory
        {
            get;
            set;
        }

        public Hashtable StopWords
        {
            get;
            set;
        }

        public bool IsSearcher
        {
            get;
            set;
        }
        /// <summary>
        /// 重载TokenStream方法,使用htmlFilter过滤特殊字符,
        /// 并分别调用MMTokenizer\MMKeywordFilter\MMSegmentFilter\SimpleChineseFilter\StopFilter实现分词
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="reader"></param>
        /// <returns></returns>
        public override TokenStream TokenStream(string fieldName, TextReader reader)
        {
            TokenStream result;
            Filter htmlFilter = null;
            string html;

            htmlFilter = FilterFactory.GetFilter("Html");
            html = htmlFilter.Execute(reader.ReadToEnd());
            
            result = new MMTokenizer(this, new StringReader(html));
            result = new MMKeywordFilter(this, result, "HR");
            result = new MMKeywordFilter(this, result, "Fish");
            //result = new MMSegmentFilter(this, result, "Fish");
            result = new MMSegmentFilter(this, result, "Normal");
            result = new SimpleChineseFilter(result, IsSearcher); // ^ punc
            result = new StopFilter(true, result, StopWords);     // have no influence on Token's offset
            return result;
        }
    }
}
